package main.java.person.fjsp.common.utils;

import main.java.person.fjsp.common.entity.DataManager;
import main.java.person.fjsp.common.entity.Operation;
import main.java.person.fjsp.common.entity.ScheduleResult;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class ProcessDataUtils {


    /**
     * 读入数据集
     */
    public DataManager readFileToJobList(String filePath) throws IOException {
        List<String> lines = new LinkedList<>(Files.readAllLines(Paths.get(filePath)));
        //去除空行
        lines.removeIf(s -> s.trim().replace(" ", "").isEmpty());
        String firstLine = lines.remove(0).replace("\t"," ").replace("   "," ").replace("  "," ");
        //作业数和机器数
        String[] jobNumAndMachineNum = firstLine.split(" ");
        int jobNum = Integer.parseInt(jobNumAndMachineNum[0]);
        int machineNum = Integer.parseInt(jobNumAndMachineNum[1]);
        Map<String, List<Integer>> operationAndMachineListMap = new HashMap<>();
        Map<String, Integer> operationProcessTimeMap = new HashMap<>();

        List<ArrayList<Integer>> collect = lines.stream()
                .map(line -> line.trim().replace("   "," ").replace("  ", " ").replace("\t"," ").split(" "))
                .map(arr -> {
                    ArrayList<Integer> list = new ArrayList<>(arr.length);
//                    System.out.println(Arrays.toString(arr));
                    for (String s : arr) {
//                        System.out.println(s);
                        list.add(Integer.parseInt(s));
                    }
                    return list;
                }).toList();
        List<String> operations = new ArrayList<>();
        for (int i = 0; i < collect.size(); i++) {
            int j = 1;
            int operationOrder = 0;
            while (j < collect.get(i).size()) {
                String operationCode = i + "-" + operationOrder;
                int machineNumForOperation = collect.get(i).get(j);
                operationAndMachineListMap.put(operationCode, new ArrayList<>(machineNumForOperation));
                operations.add(operationCode);
                for (int k = j + 1; k < j + 1 + machineNumForOperation * 2; k += 2) {
                    //k是机器编号，k+1是工序在这台机器上的处理时间
                    operationAndMachineListMap.get(operationCode).add(collect.get(i).get(k) - 1);
                    operationProcessTimeMap.put(operationCode + "-" + (collect.get(i).get(k) - 1), collect.get(i).get(k + 1));
                }
                j += (machineNumForOperation * 2) + 1;
                operationOrder++;
            }
        }
        Map<String, Integer> operationCodeAndIndexMap = new HashMap<>(operations.size());
        for (int i = 0; i < operations.size(); i++) {
            operationCodeAndIndexMap.put(operations.get(i), i);
        }
        return new DataManager(jobNum, machineNum, operationAndMachineListMap.size(), operations,
                operationAndMachineListMap, operationProcessTimeMap, operationCodeAndIndexMap);
    }

    /**
     * 将调度计划写入txt文件
     */
    public void writeSchedule(String filepath, ScheduleResult scheduleResult) throws IOException {
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < scheduleResult.getMachines().size(); i++) {
            for (Operation operation : scheduleResult.getMachines().get(i).getOperations()) {
                content.append("(")
                        .append(operation.getCode())
                        .append(",")
                        .append(operation.getReleaseTime())
                        .append(",")
                        .append(operation.getCompleteTime())
                        .append(")");
            }
            if(i!=scheduleResult.getMachines().size()-1){
                content.append("|");
            }
        }
        Files.writeString(Paths.get(filepath),content);

    }
}
